<?php

class My
{
    /*七牛上传服务器的放方法*/
    public function upload()
    {
        Qiniu 位置  vendor 下

    use Qiniu\Auth;
    use Qiniu\Storage\UploadManager;

        // 获取上传图片信息
        $file = request()->file('file');

        // var_dump($file);
        // exit;
        // 图片存储在本地的临时路经
        $filePath = $file->getRealPath();
        $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
        // 获取图片后缀
        // $ext = $file->getOriginalExtension();
        // 上传到七牛后保存的新图片名
        $imgkey = date('YmdHis') . rand(00000, 99999) . '.' . $ext;
        $newImageName = date('Y') . '/' . date('m') . '/' . $imgkey;
        // var_dump($newImageName);
        // exit;
        // 说明：为了方便阅读，上一行代码进行的回车，如果你的遇到了问题，请放到一行
        // 构建鉴权对象
        $auth = new Auth('4V-DH8KPZxV0s774dXSF3KpUWfVWjOLbidqOF1e2', 'WEOLw37hBiJANUFbZzhMCDEaBkSibbXCwOReq0BG');
        // 要上传的空间位置
        $token = $auth->uploadToken('xiankeqingchuang');
        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        list($ret, $err) = $uploadMgr->putFile($token, $newImageName, $filePath);
        if ($err !== null) {
            $aliyunOss = \Api\AliyunOss::instance([
                'AccessKey' => SystemConfigService::get('accessKeyId'),
                'AccessKeySecret' => SystemConfigService::get('accessKeySecret'),
                'OssEndpoint' => SystemConfigService::get('end_point'),
                'OssBucket' => SystemConfigService::get('OssBucket'),
                'uploadUrl' => SystemConfigService::get('uploadUrl'),
            ]);
            $res = $aliyunOss->upload('file');
            if ($res && isset($res['url'])) {
                return JsonService::successful('上传成功', ['url' => $res['url']]);
            } else {
                return JsonService::fail('上传失败');
            }
        } else {
            $url = 'https://video.file.zhuochengyun.com/';
            $reurl = $url . $newImageName;
            return JsonService::successful('上传成功', ['url' => $reurl]);
        }
    }
}